package com.bug.mq.chapter12;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@Slf4j
@RequestMapping("users")
public class UserController {
    static Map<Long,User> users = Collections.synchronizedMap(new HashMap<>());

    @GetMapping
    @ApiOperation(value = "用户列表")
    public List<User> list(){
        List<User> list = Lists.newArrayList();
        return list;
    }

    @PostMapping
    @ApiOperation(value = "创建用户",notes = "根据User对象创建用户")
    public String insertUser(@RequestBody User user){
        users.put(user.getUserId(),user);
        return "ok";
    }

    @ApiOperation(value = "获取用户详情",notes = "根据用户ID")
    @GetMapping("/{userId}")
    public User detail(@PathVariable Long userId){
        return users.getOrDefault(userId,User.builder().build());
    }

    @ApiImplicitParam(paramType = "path",dataType = "Long",name = "userId",value = "用户编码",required = true,example = "1")
    @ApiOperation(value = "修改用户",notes = "根据userId指定更新对象，并通过传入User对象进行更新")
    @PutMapping("/{userId}")
    public String updateUser(@PathVariable Long userId,@RequestBody User user){
        User updateUser = users.getOrDefault(userId,User.builder().build());
        updateUser.setName(updateUser.getName());
        updateUser.setUserId(updateUser.getUserId());
        users.put(userId,updateUser);
        return "ok";
    }



}
